﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;
using System.Collections;

namespace DRHotel_v1.HousekeepingAndStaffMngmt
{
    public partial class Housekeeping : Form
    {
        public Housekeeping()
        {
            InitializeComponent();
            cbxSearchBy.SelectedIndex = 0;
            cbxSearchBy2.SelectedIndex = 0;
        }

        private void cbxSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy.SelectedItem.ToString();
            
            //Defining what the user search by
            if (searchBy.Equals("Staff ID"))
            {
                ArrayList staffList = HousekeepingDBManager.GetStaffList();
                cbxSearch.Items.Clear();

                for (int i = 0; i < staffList.Count; i++)
                {
                    Staff staff2 = (Staff)staffList[i];
                    cbxSearch.Items.Add(staff2.staffID);
                }
            }
            else if (searchBy.Equals("Staff Name"))
            {
                ArrayList staffList = HousekeepingDBManager.GetStaffList();
                cbxSearch.Items.Clear();

                for (int i = 0; i < staffList.Count; i++)
                {
                    Staff staff2 = (Staff)staffList[i];
                    string staffName = staff2.firstName + " " + staff2.lastName;
                    cbxSearch.Items.Add(staffName);
                }
            }
        }

        private void lblSearchBy2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy2.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy.Equals("Room ID"))
            {
                ArrayList roomList = HousekeepingDBManager.GetScheduledRoomList();
                cbxSearch2.Items.Clear();

                for (int i = 0; i < roomList.Count; i++)
                {
                    Room room2 = (Room)roomList[i];
                    cbxSearch2.Items.Add(room2.roomID);
                }
            }
        }

        private void btnAssign_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to assign this staff to the room?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion

                #region declaring variables
                //Declaring all the variables
                string value = Convert.ToString(cbxSearch.SelectedItem);
                string type = cbxSearchBy.SelectedItem.ToString();
                string value3 = Convert.ToString(cbxSearch2.SelectedItem);
                string type3 = cbxSearchBy2.SelectedItem.ToString();
                DateTime now = DateTime.Now;
                Staff staff = new Staff();
                Room room = new Room();
                HousekeepingRecord housekeeping= new HousekeepingRecord();
                #endregion

                #region get staff
                if (type.Equals("Staff ID"))
                {
                    int value2 = Convert.ToInt32(value);
                    staff = HousekeepingDBManager.GetStaffByID(value2);
                }
                else if (type.Equals("Staff Name"))
                {
                    string[] words = value.Split(' ');
                    string firstName = words[0];
                    string lastName = words[1];

                    staff = HousekeepingDBManager.GetStaffByName(firstName, lastName);
                }
                #endregion

                #region get room
                if (type3.Equals("Room ID"))
                {
                    int value4 = Convert.ToInt32(value3);
                    room = HousekeepingDBManager.GetRoomByID(value4);
                }
                #endregion 

                #region get housekeeping record
                housekeeping.staffID = staff.staffID;
                housekeeping.roomID = room.roomID;
                housekeeping.assignStaff = staff.firstName + " " + staff.lastName;
                housekeeping.dateTime = now;
                housekeeping.assignStaffType = staff.staffType;
                #endregion

                #region insert housekeeping record
                bool success = HousekeepingDBManager.InsertHousekeepingRecord(housekeeping);

                if (success == true)
                {
                    int success2 = HousekeepingDBManager.UpdateRoomByID(room.roomID, room.roomType, room.bedType, room.capacity, room.roomRates, "Cleaning", room.smoking);
                    if (success2 > 0)
                    {
                        MessageBox.Show("Housekeeping record created successfully!");

                        #region refresh combobox rooms
                        ArrayList roomList = HousekeepingDBManager.GetScheduledRoomList();
                        cbxSearch2.Items.Clear();

                        for (int i = 0; i < roomList.Count; i++)
                        {
                            Room room2 = (Room)roomList[i];
                            cbxSearch2.Items.Add(room2.roomID);
                        }
                        #endregion
                    }
                }
                else
                {
                    MessageBox.Show("Housekeeping record not created, please try again!");
                }
                #endregion
            }
        }

        private void cbxSearchBy3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy3.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy.Equals("Room ID"))
            {
                ArrayList roomList = HousekeepingDBManager.GetScheduledRoomList2();
                cbxSearch3.Items.Clear();

                for (int i = 0; i < roomList.Count; i++)
                {
                    Room room2 = (Room)roomList[i];
                    cbxSearch3.Items.Add(room2.roomID);
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to finish this housekeeping record to the room?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion

                #region declaring variables
                //Declaring all the variables
                string value = Convert.ToString(cbxSearch3.SelectedItem);
                string type = cbxSearchBy3.SelectedItem.ToString();
                Room room = new Room();
                #endregion

                #region get room
                if (type.Equals("Room ID"))
                {
                    int value4 = Convert.ToInt32(value);
                    room = HousekeepingDBManager.GetRoomByID(value4);
                }
                #endregion

                #region clean housekeeping 
                int success2 = HousekeepingDBManager.UpdateRoomByID(room.roomID, room.roomType, room.bedType, room.capacity, room.roomRates, "Vacant", room.smoking);;

                if (success2 > 0)
                {
                    MessageBox.Show("Housekeeping finish successfully!");

                    #region refresh combobox rooms
                    ArrayList roomList = HousekeepingDBManager.GetScheduledRoomList2();
                    cbxSearch3.Items.Clear();

                    for (int i = 0; i < roomList.Count; i++)
                    {
                        Room room2 = (Room)roomList[i];
                        cbxSearch3.Items.Add(room2.roomID);
                    }
                    #endregion
                }
                else
                {
                    MessageBox.Show("Housekeeping not finish, please try again!");
                }
                #endregion
            }
        }

    }
}
